package com.djsearch.ui;


import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.djsearch.R;
import com.djsearch.R.string;
import com.djsearch.controller.AsyncHandler;
import com.djsearch.controller.DefaultController;
import com.djsearch.controller.IRequest;
import com.djsearch.controller.IResponse;
import com.djsearch.controller.JSONReq;
import com.djsearch.controller.JSONResp;
import com.djsearch.data.HttpRequestManager;

/**
 * 一键注册
 * 
 * @author yinxing
 * 
 */
public class RegisterActivity extends BaseActivity {
	
	private static DefaultController HTTP = DefaultController.getHttpClient();
//	private EditText bianHaoEdit,passWordEdit,introPersonEdit;//编号（用户名），密码，推荐人  编辑框
    private TextView registerText;//注册按钮
    private Button btnBack;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(
				WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
		setContentView(R.layout.register_layout);
		initPages();
		initOnClick();
		requestData("registerReplay","正在一键注册");
	}

	private void initPages(){
		btnBack = (Button)findViewById(R.id.btnBack);
//		bianHaoEdit = (EditText)findViewById(R.id.member_No);
//		passWordEdit = (EditText)findViewById(R.id.password);
//		introPersonEdit = (EditText)findViewById(R.id.tuijianren);
		registerText = (TextView)findViewById(R.id.register);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			startActivity(RegisterActivity.this, LoginActivity.class, 1);
		}
		return false;
	}

	@Override
	void initHandler() {
		// TODO Auto-generated method stub
		
	}

	@Override
	void initView() {
		// TODO Auto-generated method stub
		
	}

	@Override
	void initOnClick() {
		// TODO Auto-generated method stub
		btnBack.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(RegisterActivity.this, null, 1);
			}
		});
		registerText.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
//				if (bianHaoEdit.getText().toString().equals("")) {
//					startAnimation(bianHaoEdit);
//				} else if (passWordEdit.getText().toString().equals("")) {
//					startAnimation(passWordEdit);
//				} else {
//					Map<String, String> map = new HashMap<String, String>();
//					map.put("userId", bianHaoEdit.getText().toString());
//					map.put("password", passWordEdit.getText().toString());
//					map.put("recommendPerson", introPersonEdit.getText().toString());
//					requestRegister("register", "正在注册...",map);
//				}
				
			}
		});
	}
	
	/**
	 * 一键注册
	 * @param urlKey
	 * @param promtMessage
	 */
	private void requestData(String urlKey,String promtMessage){
		
		IRequest request = new JSONReq(urlKey);
		showDialog(promtMessage);
		HTTP.post(request, new AsyncHandler(){
			@Override
			public void onSuccess(JSONResp response) {
				// TODO Auto-generated method stub
				int result = response.result();
				if(IResponse.Success==result){
					//请求成功
					missDialog();
					getDataToResolve(response);
					
					
				}else if(IResponse.Fail==result){
					//请求失败
					missDialog();
				}
			}
			@Override
			public void onFailure(Throwable e, String response) {
				super.onFailure(e, response);
			}
			
		});
	}

	/**
	 * 开始注册
	 * @param urlKey 请求连接
	 * @param promtMessage 对话框提示
	 * @param map 发送到服务端 字段 map集合
	 */
	private void requestRegister(String urlKey,String promtMessage,Map<String,String> map) {
		IRequest request = new JSONReq(urlKey);
		showDialog(promtMessage);
		request.putParam("userId", map.get("userId"));
		request.putParam("password", map.get("password"));
		request.putParam("recommendPerson", map.get("recommendPerson"));
		HTTP.post(request, new AsyncHandler(){
			@Override
			public void onSuccess(JSONResp response) {
				// TODO Auto-generated method stub
				int result = response.result();
				if(IResponse.Success==result){
					//请求成功
					missDialog();
					try {
						/**
						 * 请求成功后相关处理
						 */
						String returnMessage = response.getString("message");
						Toast.makeText(RegisterActivity.this, returnMessage, 5000).show();
						startActivity(RegisterActivity.this, LoginActivity.class, 1);
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}else if(IResponse.Fail==result){
					//请求失败
					missDialog();
				}
			}
			@Override
			public void onFailure(Throwable e, String response) {
				super.onFailure(e, response);
				//请求失败
				missDialog();
			}
			
		});
	
	}
	
	@Override
	void getDataToResolve(JSONResp response) {
		// TODO Auto-generated method stub
		try {
			String a = response.getString("password");
			String b = response.getString("userId");
			Toast.makeText(RegisterActivity.this, b, 5000).show();
//			bianHaoEdit.setText(a); 
//			passWordEdit.setText(b); 
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
